{extend name="base" /}
{block name="body"}
<style>
    .layui-form-switch {
        margin-top: 0;
    }
</style>
<div class="admin-main">
    <div class="layui-field-box layui-form">
        <a href="{:url('admin/department/add')}" class="layui-btn layui-btn-sm" id="add">
           <!-- <i class="layui-icon">&#xe608;</i> -->添加部门
        </a>
        <table class="layui-table admin-table ">
            <thead>
            <tr>
                <th style="width: 30px;">ID</th>
                <th>名称</th>
                <th>是否显示</th>
                <th>排序</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {foreach name="department_level_list" item="vo"}
            <tr>
                <td>{$vo.id}</td>
                <td>
                    {$vo.name|raw}
                </td>
                <td><input type="checkbox" name="status" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switchTest"
                           value="{$vo['id']}" {php}echo $vo['show']==1 ? 'checked' : '';{/php}>
                </td>
                <td>
                    <input style="width: 100px;height: 28px;" type="text" name="sort" autocomplete="off" lay-verify="" placeholder="请输入排序数值" value="{$vo.sort}" data-sort="{$vo.sort}" data-id="{$vo.id}" class="layui-input">
                </td>
                <!--<td>{$vo.sort}</td>-->
                <td>
                    <a href="{:url('admin/department/add',['pid'=>$vo['id']])}" class="layui-btn  layui-btn-warm layui-btn-xs">添加子部门</a>
                    <a href="{:url('admin/department/edit',['id'=>$vo['id']])}"
                       class="layui-btn layui-btn-normal layui-btn-xs">编辑</a>
                    <a href="javascript:;" data-id="{$vo.id}"
                       data-url="{:url('admin/department/delete',array('id'=>$vo['id']))}" data-opt="del"
                       class="layui-btn layui-btn-danger layui-btn-xs ajax-delete">删除</a>
                </td>
            </tr>
            {/foreach}
            </tbody>
        </table>
    </div>
</div>

{/block}
{block name="js"}
<script type="text/javascript" src="__PUBLIC__/js/delelement.js"></script>
{/block}
{block name="script"}
<script>
    layui.use(['form','jquery'], function () {
        var form = layui.form
            , jq = layui.jquery;


        //jq('.btable-paged').eq(1).hide();
        var status = 0;
        form.on('switch(switchTest)', function (data) {
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            if (data.elem.checked) {
                status = 1;
            } else {
                status = 0;
            }
            var url = "{:url('admin/department/updateStatus')}?id=" + data.value + '&show=' + status;

            jq.get(url, function (data) {

                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                        //  location.reload();
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
                }
            });
            return false;
        });

        jq("input[name='sort']").blur(function () {
            console.log('sort.blur');
            let sort = jq(this).data('sort');
            let id = jq(this).data('id');
            let value = jq(this).val();
            console.log('sort: ', sort)
            if (value == '') {
                jq(this).val(sort);
                return false;
            }
            if (sort == value) return false;
            let param = {
                id: id,
                field: 'sort',
                value: value
            }
            let that = jq(this);
            console.log(param);
            jq.post('updateField', param, function (res) {
                if (res.code == 200) {
                    console.log('200');
                    that.data('sort', value);
                } else {

                }
                layer.msg(res.msg);
            });
        });

        jq('.ajax-delete').click(function(){
            var obj = jq(this);
            var url= jq(this).data('url');
            layer.confirm('确定删除该条数据吗?',{icon: 3, title:'删除提示'}, function (index) {
                jq.post(url, {}, function (res) {
                    if (res.code != 1) {
                        return layer.alert(res.msg, {icon: 2, time: res.wait*1000});
                    } else {
                        layer.msg(res.msg);
                        setTimeout(function () {
                            obj.closest('tr').remove();
                            layer.close(index);
                        }, 300);
                    }
                });
            });
        });
    })
</script>
{/block}